package com.example.message.controller;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.message.entity.MessageTemplate;
import com.example.message.service.MessageTemplateService;
import com.example.project.core.controller.BaseController;
import com.example.project.core.domain.AjaxResult;
import com.example.project.core.page.TableDataInfo;

/**
 * 通知模板控制器
 */
@RestController
@RequestMapping("/message/template")
public class MessageTemplateController extends BaseController {

	private String prefix = "system/message";

    @Autowired
    private MessageTemplateService messageTemplateService;

	@RequiresPermissions("system:message:template:view")
	@GetMapping()
	public String notice() {
		return prefix + "/template_list";
	}
    /**
     * 获取通知模板列表
     */
    @GetMapping("/list")
    public TableDataInfo list(MessageTemplate messageTemplate) {
        startPage();
        List<MessageTemplate> list = messageTemplateService.selectNoticeTemplateList(messageTemplate);
        return getDataTable(list);
    }

    /**
     * 根据模板ID获取详细信息
     */
    @GetMapping(value = "/{templateId}")
    public AjaxResult getInfo(@PathVariable Long templateId) {
        return success(messageTemplateService.selectNoticeTemplateById(templateId));
    }

    /**
     * 新增通知模板
     */
    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody MessageTemplate messageTemplate) {
        messageTemplate.setCreateBy(getUserId());
        return toAjax(messageTemplateService.insertNoticeTemplate(messageTemplate));
    }

    /**
     * 修改通知模板
     */
    @PutMapping("/edit")
    public AjaxResult edit(@Validated @RequestBody MessageTemplate messageTemplate) {
        messageTemplate.setUpdateBy(getUserId());
        return toAjax(messageTemplateService.updateNoticeTemplate(messageTemplate));
    }

    /**
     * 删除通知模板
     */
    @DeleteMapping("/remove/{templateIds}")
    public AjaxResult remove(@PathVariable Long[] templateIds) {
        return toAjax(messageTemplateService.deleteNoticeTemplateByIds(templateIds));
    }

    /**
     * 预览模板
     */
    @GetMapping("/preview/{templateId}")
    public AjaxResult preview(@PathVariable Long templateId) {
        MessageTemplate template = messageTemplateService.selectNoticeTemplateById(templateId);
        return success(template);
    }

    /**
     * 根据模板编码获取模板
     */
    @GetMapping("/code/{templateCode}")
    public AjaxResult getByCode(@PathVariable String templateCode) {
        MessageTemplate template = messageTemplateService.selectNoticeTemplateByCode(templateCode);
        return success(template);
    }

    /**
     * 渲染模板内容
     */
    @PostMapping("/render/{templateCode}")
    public AjaxResult renderTemplate(@PathVariable String templateCode, @RequestBody String variables) {
        String content = messageTemplateService.renderTemplate(templateCode, variables);
        return success(content);
    }
}